package com.yc.javaee.servlet.d0613;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 地址错误会直接导致服务器启动失败
 * 1. / 开头, 精确映射
 * 2. *.jpg  后缀名映射
 */
@WebServlet("/add.s")
public class AddServlet extends HttpServlet {

    // http://127.0.0.1:80/工程名/add.s?a=1&b=2
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String a = req.getParameter("a");
        String b = req.getParameter("b");
        Integer a1 = Integer.valueOf(a);
        Integer b1 = Integer.valueOf(b);
        // 返回类型
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.append("<h1>").append(a1 + b1 + "").append("<h1>");
        System.out.println("========doGet 服务 每次访问是执行=======");
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String a = req.getParameter("a");
        String b = req.getParameter("b");
        Integer a1 = Integer.valueOf(a);
        Integer b1 = Integer.valueOf(b);
        // 返回类型
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.append("<h1 style='color:red'>").append(a1 * b1 + "").append("<h1>");
        System.out.println("========doGet 服务 每次访问是执行=======");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("========init 初始化 第一次访问是创建并初始化=======");
    }

    @Override
    public void destroy() {
        System.out.println("========destroy 销毁 服务器停止时销毁=======");
    }
}
